package com.example.quartz.bean.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.quartz.bean.entity.QrtzJobDetailsEntity;
import com.example.quartz.bean.entity.QuartzEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @author: luojm
 * @create: 2020-07-27 14:35:02
 */
@Mapper
public interface QrtzJobDetailsMapper extends BaseMapper<QrtzJobDetailsEntity> {
    @Select({
            "SELECT " +
            "job.JOB_NAME AS jobName, " +
            "job.JOB_GROUP AS jobGroup, " +
            "job.DESCRIPTION AS description, " +
            "job.JOB_CLASS_NAME AS jobClassName, " +
            "cron.CRON_EXPRESSION AS cronExpression, " +
            "tri.TRIGGER_NAME AS triggerName, " +
            "tri.TRIGGER_STATE AS triggerState, " +
            "job.JOB_NAME AS oldJobName, " +
            "job.JOB_GROUP AS oldJobGroup  " +
            "FROM " +
            "qrtz_job_details AS job " +
            "LEFT JOIN qrtz_triggers AS tri ON job.JOB_NAME = tri.JOB_NAME  " +
            "AND job.JOB_GROUP = tri.JOB_GROUP " +
            "LEFT JOIN qrtz_cron_triggers AS cron ON cron.TRIGGER_NAME = tri.TRIGGER_NAME  " +
            "AND cron.TRIGGER_GROUP = tri.JOB_GROUP  " +
            "WHERE " +
            "tri.TRIGGER_TYPE = 'CRON'  " +
            "<if test='jobName != null'> " +
            "AND job.JOB_NAME LIKE CONCAT('%',#{jobName},'%') " +
            "</if> "
    })
    Page<QuartzEntity> listQuartz(Page<QuartzEntity> page, @Param("jobName") String jobName);

    @Select({
            "SELECT COUNT(*) " +
            "FROM qrtz_job_details AS job LEFT JOIN qrtz_triggers AS tri ON job.JOB_NAME = tri.JOB_NAME " +
            "LEFT JOIN qrtz_cron_triggers AS cron ON cron.TRIGGER_NAME = tri.TRIGGER_NAME " +
            "WHERE tri.TRIGGER_TYPE = 'CRON'"
    })
    Long countQuartz();
}
